home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / archive / userbox / publicdomain / engclock_v7.0.lha / EngClock_v7.0 / EngClock7_Source / special.c < prev    next >
C/C++ Source or Header  |  1996-05-19  |  14KB  |  615 lines

  1. /* Special "access" routines */
  2.  
  3. #include <clib/exec_protos.h>
  4. #include <clib/wb_protos.h>
  5. #include <clib/alib_protos.h>
  6. #include <clib/commodities_protos.h>
  7. #include <clib/intuition_protos.h>
  8. #include <clib/dos_protos.h>
  9. #include <libraries/commodities.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <intuition/intuition.h>
  14. #include <clib/intuition_protos.h>
  15. #include <exec/exec.h>
  16. #include <intuition/screens.h>
  17. #include <rexx/storage.h>
  18. #include <clib/rexxsyslib_protos.h>
  19. #include <libraries/arq.h>
  20. #include <clib/icon_protos.h>
  21. #include <workbench/startup.h>
  22. #include <workbench/workbench.h>
  23.  
  24. /* #define DEBUG_CODE */
  25.  
  26. #define ENGLISH 1
  27. #define FRENCH 2
  28. #define SPANISH 3
  29.  
  30. struct Message *a_msg=NULL;
  31. struct MsgPort *CxMsgPort=NULL, *CxHotKey1=NULL, *CxHotKey2=NULL, *RexxPort=NULL;
  32. CxObj *CommObj, *hk1, *hk2;
  33. CxMsg *CxMessage;
  34.  
  35. struct Library *IconBase=NULL;
  36.  
  37. extern struct Window *win_p;
  38. extern BOOL talk(char *text);
  39. extern char line2[200];
  40. extern char line1[200];
  41. extern struct Menu menu1;
  42. extern BOOL loadpic(char *fname, struct Screen *screen);
  43. extern void refreshwindow(void);
  44. extern char *trans(char *text);
  45. extern BOOL ispicture;
  46. extern int language;
  47. extern int prefsgui(void);
  48. extern void changemenu(void);
  49. extern BOOL exit_flag;
  50. extern void msg(char *msg);
  51. extern struct NewWindow win1;
  52. extern void closeall(void);
  53. extern void setupmainwindow(void);
  54.  
  55. extern struct Screen *pubscreen;
  56. extern struct Library *CxBase;
  57. extern struct IntuitionBase *IntuitionBase;
  58. extern struct GfxBase *GfxBase;
  59. extern struct Library *UtilityBase;
  60. extern struct Library *GadToolsBase;
  61. extern struct Library *DiskfontBase;
  62. extern struct Library *AslBase;
  63. extern struct Library *IFFParseBase;
  64. extern struct Library *DOSBase;
  65. extern struct Library *RexxSysBase;
  66.  
  67. BOOL cxinit(void);
  68. BOOL arexxinit(void);
  69. void cxclose(void);
  70. void arexxclose(void);
  71. void process_arexx(void);
  72. void process_args(int count, char *args[]);
  73. void process_wbargs(struct WBStartup *wbs);
  74.  
  75. struct Event {
  76.     BOOL used;
  77.     int type;    /* Type of event */
  78.     int hours;    /* Time of event */
  79.     int minutes;
  80.     int timecode;
  81.     char message[256];
  82.     int day;
  83.     int month;
  84.     int year;
  85.     BOOL enabledate;
  86.     int freq;
  87. };
  88.  
  89. extern struct {
  90. /* Prefs structure used for preferences file format */
  91.  
  92.    char header[10];  /* I.D. Header */
  93.    int vers;         /* Version of preferences file */
  94.    int x;            /* X dimension of window when saved */
  95.    int y;            /* Y dimension of window when saved */
  96.    int width;        /* Width of window when saved */
  97.    int height;       /* Height of window when saved */
  98.    int planguage;    /* Language selected (menu) when saved, low pri */
  99.    int just;         /* Justification of text, 0=Left, 1=Centre, 2=Right */
  100.    short date;       /* Display date?, 0 = No, 1=Yes */
  101.    short wtf;         /* Window to front ? */
  102.    int time_col[8];
  103.    int date_col[8];
  104.    short autoadjust;       /* Use automatic adjustment on window ? */
  105.    char backdrop[256];  /* Backdrop image */
  106.    char pub[139];            /* Default public screen */
  107.     char accent[256];        /* Language file for tRanslate */
  108.     char tkey[100];
  109.     char hkey[100];
  110.     struct Event events[11];  /* For the alarm! */
  111.  
  112. }prefs;
  113.  
  114. static struct NewBroker newbrok = {
  115.     NB_VERSION,
  116.     "EngClock 1",
  117.     "EngClock v6.6 © 1995 by Ben Matthew",
  118.     "A clock with words!",
  119.     NBU_UNIQUE|NBU_NOTIFY,
  120.     COF_SHOW_HIDE,
  121.     0,
  122.     0,
  123.     0
  124. };
  125.  
  126. BOOL cxinit(void) {
  127.     char comtext[50], tmp[10];
  128.     int count=1;
  129.     
  130.     #define MAXBROKERS 10
  131.  
  132.     if(!CxBase) 
  133.         return(0);
  134.  
  135.     CxMsgPort=CreateMsgPort();
  136.     CxHotKey1=CreateMsgPort();
  137.     CxHotKey2=CreateMsgPort();
  138.  
  139.     if(!(CxMsgPort && CxHotKey1 && CxHotKey2)) 
  140.         return(0);
  141.     
  142.     newbrok.nb_Port=CxMsgPort;
  143.     while(!(CommObj=CxBroker(&newbrok,NULL))) {
  144.         count++;
  145.         if(count==MAXBROKERS) break;
  146.         strcpy(comtext,"EngClock ");
  147.         sprintf(tmp,"%d",count);
  148.         strcat(comtext,tmp);
  149.         newbrok.nb_Name=comtext;
  150.     }
  151.     if(!CommObj) {
  152.         
  153.         DeleteMsgPort(CxMsgPort);
  154.         CxMsgPort=NULL;
  155.         return(0);
  156.     } else
  157.         ActivateCxObj(CommObj,1L);
  158.  
  159.     
  160.     hk1=HotKey(prefs.tkey,CxHotKey1,1234);
  161.     hk2=HotKey(prefs.hkey, CxHotKey2,9999);
  162.  
  163.     if(!(hk1 && hk2)) {
  164.         if(hk1) DeleteCxObj(hk1);
  165.         if(hk2) DeleteCxObj(hk2);
  166.         DeleteMsgPort(CxHotKey1);
  167.     } else {
  168.         AttachCxObj(CommObj,hk1);
  169.         AttachCxObj(CommObj,hk2);
  170.         ActivateCxObj(CommObj,1L);
  171.     }    
  172.     
  173.     return(1);
  174. }
  175.  
  176. void cxclose(void) {
  177.  
  178.     if(CommObj) 
  179.         DeleteCxObjAll(CommObj);
  180.  
  181.     
  182.     if(CxMsgPort) {
  183.         while(CxMessage=(CxMsg *)GetMsg(CxMsgPort)) 
  184.             ReplyMsg((struct Message *)CxMessage);
  185.         
  186.         DeleteMsgPort(CxMsgPort);
  187.         CxMsgPort=NULL;
  188.     }
  189.  
  190.     if(CxHotKey1) {
  191.         while(CxMessage=(CxMsg *)GetMsg(CxHotKey1)) 
  192.             ReplyMsg((struct Message *)CxMessage);
  193.         
  194.         DeleteMsgPort(CxHotKey1);
  195.         CxHotKey1=NULL;
  196.     }
  197.  
  198.     if(CxHotKey2) {
  199.         while(CxMessage=(CxMsg *)GetMsg(CxHotKey2)) 
  200.             ReplyMsg((struct Message *)CxMessage);
  201.         
  202.         DeleteMsgPort(CxHotKey2);
  203.         CxHotKey2=NULL;
  204.     }
  205.  
  206.     return;
  207. }
  208.  
  209. /* Now a simple routine that is used in main() to check what the user 
  210. wants to do about a menu selection */
  211.  
  212. BOOL wb_ask(char *body, char *opt1, char *opt2) {
  213.     struct ExtEasyStruct deadeasy;
  214.     char string[500];
  215.     char params[100];
  216.  
  217.     strcpy(string,"English Clock v6.9\n\n");
  218.     strcat(string,body);
  219.     strcpy(params,opt2);
  220.     strcat(params,"|");
  221.     strcat(params,opt1);
  222.  
  223.     deadeasy.Magic=ARQ_MAGIC;
  224.     deadeasy.AnimID=ARQ_ID_QUESTION;
  225.     deadeasy.Easy.es_Title=" ";
  226.     deadeasy.Easy.es_TextFormat=string;
  227.     deadeasy.Easy.es_GadgetFormat=params;
  228.  
  229.     return(EasyRequest(NULL,&deadeasy.Easy,NULL,NULL));
  230.  
  231. }
  232.  
  233. BOOL arexxinit(void) {
  234.     Forbid();
  235.     if(FindPort("ENG_REXX")) {
  236.         Permit();
  237.         return(0);
  238.     }
  239.     if(!(RexxPort=(struct MsgPort *)CreatePort("ENG_REXX",0))) {
  240.         Permit();
  241.         return(0);
  242.     }
  243.     Permit();
  244.     
  245.     // Best to set up RexxBase as well
  246.  
  247.     if(!(RexxSysBase=OpenLibrary("rexxsyslib.library",0))) {
  248.         DeletePort(RexxPort);
  249.         return(0);
  250.     }
  251.             
  252.     // And thats it!
  253.  
  254.     return(1);
  255.  
  256. }
  257.  
  258. void arexxclose(void) {
  259.     struct Message *a_mess=NULL;
  260.  
  261.     while(a_mess=GetMsg(RexxPort))
  262.         ReplyMsg(a_mess);
  263.     
  264.     Forbid();
  265.     DeletePort(RexxPort);
  266.     Permit();
  267.     RexxPort=NULL;
  268.  
  269.     if(RexxSysBase) CloseLibrary(RexxSysBase);
  270.     RexxSysBase=NULL;
  271. }
  272.  
  273. void process_arexx(void) {
  274.     struct RexxMsg *rexxmsg=NULL;
  275.     char string[50];
  276.     char newspeak[200];
  277.     char *ptr, *ptr2;
  278.     char string2[50];
  279.     BPTR file;
  280.     int x, y, width, height, count=7, gotted=0, len;
  281.  
  282.     while(rexxmsg=(struct RexxMsg *)GetMsg(RexxPort)) {
  283.  
  284.         // We have a message, but what is it!
  285.  
  286.         ptr=CreateArgstring(rexxmsg->rm_Args[0],strlen(rexxmsg->rm_Args[0]));
  287.         sprintf(string,"%s",ptr);
  288.  
  289.         if(!strcmp(string,"SPEAK")) {
  290.             strcpy(newspeak,line1);
  291.             if(language==ENGLISH) newspeak[strlen(newspeak)-4]=NULL;  /* Get rid of pm/am */
  292.             strcat(newspeak,".  ");
  293.             strcat(newspeak,line2);
  294.             newspeak[strlen(newspeak)-6]=NULL; /* Get rid of year */
  295.             talk(trans(newspeak));
  296.         }    
  297.         if(!strcmp(string,"PREFS")) {
  298.             prefsgui();
  299.             ClearMenuStrip(win_p);
  300.             changemenu();
  301.             SetMenuStrip(win_p,&menu1);
  302.             ispicture=loadpic(prefs.backdrop,pubscreen);
  303.             refreshwindow();
  304.         }
  305.         if(!strcmp(string,"ACTIVATE")) {
  306.             WindowToFront(win_p);
  307.         }
  308.         if(!strcmp(string,"QUIT")) {
  309.             exit_flag=TRUE;
  310.         }
  311.         if(!strcmp(string,"LEFT")) {
  312.             prefs.just=0;
  313.             refreshwindow();
  314.         }
  315.         if(!strcmp(string,"RIGHT")) {
  316.             prefs.just=2;
  317.             refreshwindow();
  318.         }
  319.         if(!strcmp(string,"CENTRE")) {
  320.             prefs.just=1;
  321.             refreshwindow();
  322.         }
  323.         if(!strcmp(string,"ENGLISH")) {
  324.             language=ENGLISH;
  325.             refreshwindow();
  326.             ClearMenuStrip(win_p);
  327.             changemenu();
  328.             SetMenuStrip(win_p,&menu1);
  329.         }
  330.         if(!strcmp(string,"FRENCH")) {
  331.             language=FRENCH;
  332.             refreshwindow();
  333.             ClearMenuStrip(win_p);
  334.             changemenu();
  335.             SetMenuStrip(win_p,&menu1);
  336.         }
  337.         if(!strcmp(string,"SPANISH")) {
  338.             language=SPANISH;
  339.             refreshwindow();
  340.             ClearMenuStrip(win_p);
  341.             changemenu();
  342.             SetMenuStrip(win_p,&menu1);
  343.         }
  344.         if(!strcmp(string,"AUTO ON")) {
  345.             prefs.autoadjust=1;
  346.             refreshwindow();            
  347.         }
  348.         if(!strcmp(string,"AUTO OFF")) {
  349.             prefs.autoadjust=0;
  350.             refreshwindow();            
  351.         }
  352.         if(!strcmp(string,"DATE ON")) {
  353.             prefs.date=1;
  354.             refreshwindow();            
  355.         }
  356.         if(!strcmp(string,"DATE OFF")) {
  357.             prefs.autoadjust=0;
  358.             refreshwindow();            
  359.         }
  360.         strcpy(string2,string);
  361.         string2[8]=NULL;
  362.         if(!strcmp(string2,"SENDPORT")) {
  363.             ptr2=&string2[9];
  364.             if(file=Open(ptr2,MODE_OLDFILE)) {
  365.                 Write(file,line1,strlen(line1));
  366.                 Close(file);
  367.             }
  368.         }
  369.         if(!strcmp(string2,"SENDFILE")) {
  370.             ptr2=&string2[9];
  371.             if(file=Open(ptr2,MODE_READWRITE)) {
  372.                 Write(file,line1,strlen(line1));
  373.                 Close(file);
  374.             }
  375.         }
  376.         strcpy(string2,string);
  377.         string2[6]=NULL;
  378.         if(!strcmp(string2,"SCREEN")) {
  379.             ptr2=&string2[7];
  380.             sprintf(prefs.pub,"%s",ptr2);
  381.         }
  382.         if(!strcmp(string2,"ACCENT")) {
  383.             ptr2=&string2[7];
  384.             sprintf(prefs.accent,"%s",ptr2);
  385.         }
  386.         strcpy(string2,string);
  387.         string2[5]=NULL;
  388.         if(!strcmp(string2,"PICCY")) {
  389.             ptr2=&string2[6];
  390.             sprintf(prefs.backdrop,ptr2);
  391.             ispicture=loadpic(prefs.backdrop,pubscreen);
  392.             refreshwindow();
  393.         }
  394.         strcpy(string2,string);
  395.         string2[6]=NULL;
  396.         count=7;
  397.         if(!strcmp(string2,"NEWWIN")) {
  398.             strcpy(string2,string); /* Re-init string2 */
  399.             ptr2=&string2[7];
  400.             len=strlen(string2);
  401.             while(count <= len) {
  402.                 if(string[count]==NULL)  /* End of line yet? */
  403.                     height=atoi(ptr2);
  404.                 
  405.  
  406.                 if(string2[count]==',') {
  407.                     string2[count]=NULL;
  408.  
  409.                     switch(gotted) {
  410.                         case 0:
  411.                             x=atoi(ptr2);
  412.                         break;
  413.                         case 1:
  414.                             y=atoi(ptr2);
  415.                         break;
  416.                         case 2:
  417.                             width=atoi(ptr2);
  418.                         break;
  419.                     }
  420.                     gotted++;
  421.                     ptr2=&string2[count]+1;
  422.                 }    
  423.                 
  424.                 count++;
  425.             }
  426.             if(gotted==3) {
  427.                 // If not there has been a parse error
  428.                 prefs.x=x;
  429.                 prefs.y=y;
  430.                 prefs.width=width;
  431.                 prefs.height=height;
  432.                 ClearMenuStrip(win_p);
  433.                 CloseWindow(win_p);
  434.                 setupmainwindow();
  435.                 if(!(win_p=OpenWindow(&win1))) {
  436.                     msg("Error re-opening window!");
  437.                     // Best to reply before bye
  438.                     ReplyMsg((struct Message *)rexxmsg);
  439.                     closeall();
  440.                     exit(NULL);
  441.                 }
  442.                 SetMenuStrip(win_p,&menu1);
  443.                 refreshwindow();
  444.             }
  445.         }
  446.  
  447.  
  448.         ReplyMsg((struct Message *)rexxmsg);
  449.     }
  450.     DeleteArgstring(ptr);
  451. }    
  452.     
  453.  
  454. void process_args(int count, char *args[]) {
  455.     // Quicky to process command line args
  456.  
  457.     int i=0, j=0;
  458.     char byte;
  459.     char byte_s[2];
  460.     char string[200], string2[200];
  461.  
  462.     // is it from wb?
  463.  
  464.     if(count==0)
  465.         process_wbargs((struct WBStartup *)args);
  466.  
  467.     // is there any args?
  468.     
  469.     if(count<2) 
  470.         return;
  471.  
  472.     // Right ok we have some
  473.  
  474.     for (i=1; i<count; i++) {
  475.         strcpy(string2,"");
  476.         //printf("Testing arg: %d\n",i);
  477.         strcpy(string,args[i]);
  478.         //printf("which is: %s\n",string);
  479.         for(j=0; j<strlen(args[i]); j++) {
  480.             byte=string[j];
  481.             if(byte != '=') {
  482.                 sprintf(byte_s,"%c",byte);    
  483.                 strcat(string2,byte_s);
  484.             }
  485.             else {
  486.                 //printf("found sign!\n");
  487.                 //printf("s2 is now: %s\n",string2);
  488.                 if(!(strcmp(string2,"X"))) {
  489.                     prefs.x=atoi(&string[j+1]);
  490.                     //printf("X is %d\n",atoi(&string[j+1]));
  491.                 }
  492.                 if(!(strcmp(string2,"Y"))) {
  493.                     prefs.y=atoi(&string[j+1]);
  494.                     //printf("Y is %d\n",atoi(&string[j+1]));
  495.                 }
  496.                 if(!(strcmp(string2,"WIDTH"))) {
  497.                     prefs.width=atoi(&string[j+1]);
  498.                     //printf("WIDTH is %d\n",atoi(&string[j+1]));
  499.                 }
  500.                 if(!(strcmp(string2,"HEIGHT"))) {
  501.                     prefs.height=atoi(&string[j+1]);
  502.                     //printf("HEIGHT is %d\n",atoi(&string[j+1]));
  503.                 }
  504.                  if(!(strcmp(string2,"PUBSCREEN"))) {
  505.                     strcpy(prefs.pub,&string[j+1]);
  506.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  507.                 }
  508.                 if(!(strcmp(string2,"JUSTIFICATION"))) {
  509.                     prefs.just=atoi(&string[j+1]);
  510.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  511.                 }
  512.                 if(!(strcmp(string2,"DATE"))) {
  513.                     if(!(strcmp(&string[j+1],"YES")))    
  514.                         prefs.date=1;
  515.                     else 
  516.                         prefs.date=0;
  517.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  518.                 }
  519.                 if(!(strcmp(string2,"WTF"))) {
  520.                     if(!(strcmp(&string[j+1],"YES")))    
  521.                         prefs.wtf=1;
  522.                     else 
  523.                         prefs.wtf=0;
  524.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  525.                 }
  526.                 if(!(strcmp(string2,"ADJUST"))) {
  527.                     if(!(strcmp(&string[j+1],"YES")))    
  528.                         prefs.autoadjust=1;
  529.                     else 
  530.                         prefs.autoadjust=0;
  531.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  532.                 }
  533.                 if(!(strcmp(string2,"BACKDROP"))) {
  534.                     strcpy(prefs.backdrop,&string[j+1]);
  535.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  536.                 }
  537.                 if(!(strcmp(string2,"ACCENT"))) {
  538.                     strcpy(prefs.accent,&string[j+1]);
  539.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  540.                 }
  541.                 if(!(strcmp(string2,"TALKKEY"))) {
  542.                     strcpy(prefs.tkey,&string[j+1]);
  543.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  544.                 }
  545.                 if(!(strcmp(string2,"HOTKEY"))) {
  546.                     strcpy(prefs.hkey,&string[j+1]);
  547.                     //printf("PUBSCREEN is %s\n",&string[j+1]);
  548.                 }
  549.             }
  550.         }
  551.     }
  552. }
  553.  
  554. void process_wbargs(struct WBStartup *wbs) {
  555.     struct DiskObject *dobj;
  556.     char *result;
  557.     
  558.     if(!(IconBase=OpenLibrary("icon.library",0))) {
  559.         msg("Unable to open icon library!");
  560.         return;
  561.     }
  562.  
  563.     dobj=GetDiskObject(wbs->sm_ArgList->wa_Name);    
  564.     if(!dobj) {
  565.         msg("Unable to get disk object!");
  566.         CloseLibrary(IconBase);
  567.         return;
  568.     }
  569.     
  570.     // Code to find tool tpes here
  571.     
  572.     if(result=FindToolType(dobj->do_ToolTypes,"X")) 
  573.         prefs.x=atoi(result);
  574.     if(result=FindToolType(dobj->do_ToolTypes,"Y")) 
  575.         prefs.y=atoi(result);
  576.     if(result=FindToolType(dobj->do_ToolTypes,"WIDTH")) 
  577.         prefs.width=atoi(result);
  578.     if(result=FindToolType(dobj->do_ToolTypes,"HEIGHT")) 
  579.         prefs.height=atoi(result);
  580.     if(result=FindToolType(dobj->do_ToolTypes,"JUSTIFICATION")) 
  581.         prefs.just=atoi(result);
  582.  
  583.     if(result=FindToolType(dobj->do_ToolTypes,"DATE")) {
  584.         if(!(strcmp(result,"YES"))) 
  585.             prefs.date=1;
  586.         else
  587.             prefs.date=0;
  588.     }
  589.     if(result=FindToolType(dobj->do_ToolTypes,"WTF")) {
  590.         if(!(strcmp(result,"YES"))) 
  591.             prefs.wtf=1;
  592.         else
  593.             prefs.wtf=0;
  594.     }
  595.     if(result=FindToolType(dobj->do_ToolTypes,"ADJUST")) {
  596.         if(!(strcmp(result,"YES"))) 
  597.             prefs.autoadjust=1;
  598.         else
  599.             prefs.autoadjust=0;
  600.     }
  601.     
  602.     if(result=FindToolType(dobj->do_ToolTypes,"PUBSCREEN")) 
  603.         strcpy(prefs.pub,result);
  604.     if(result=FindToolType(dobj->do_ToolTypes,"ACCENT")) 
  605.         strcpy(prefs.accent,result);
  606.     if(result=FindToolType(dobj->do_ToolTypes,"TALKKEY")) 
  607.         strcpy(prefs.tkey,result);
  608.     if(result=FindToolType(dobj->do_ToolTypes,"HOTKEY")) 
  609.         strcpy(prefs.hkey,result);
  610.     if(result=FindToolType(dobj->do_ToolTypes,"BACKDROP")) 
  611.         strcpy(prefs.backdrop,result);
  612.     
  613.     FreeDiskObject(dobj);
  614.     CloseLibrary(IconBase);
  615. }